using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace HS {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game {
        //GraphicsDeviceManager graphics;
        //SpriteBatch spriteBatch;
        //Character c; //diego lo hizo
        
        ///************* INICIO ANIMAR SPRITES1 ***************/
        ///*************** FIN ANIMAR SPRITES1 ****************/
        ///************Agregados************/
        //SpriteBatch spriteBatch3;
        //SpriteBatch spriteBatch4;
        //SpriteBatch spriteBatch5;
        //SpriteBatch spriteBatch6;

        //private TexturaAnimada SpriteTexture;
        ///**************************spriteTexture fig, 1,2,3,4****************************************************/
        //private TexturaAnimada SpriteTexture3;
        //private TexturaAnimada SpriteTexture4;
        //private TexturaAnimada SpriteTexture5;
        //private TexturaAnimada SpriteTexture6;
        ///*___________________________FIN__________________________*/

        //private Vector2 spritePos;//sprite yeti1
        //private Vector2 spritePos3;//Sprite yeti2
        //private Vector2 spritePos4; //sprite para mario
        //private Vector2 spritePos5;//bazooka
        //private Vector2 spritePos6;//tirador

        ///****************************variables para yeti1******************************/
        //private const int FRAMES = 9;// cantidad de imagenes en el sprite
        //private const int FRAMESPERSEC = 10;//tiempo de vista de la imagen
        ///***************************variables para yeti2************************/
        //private const int frames3 = 5;
        ///**************************variables para mario******************************/
        //private const int frames4 = 5;
        ///**************************variables para bazooka******************************/
        //private const int frames5 = 3;
        ///**************************variables para tirador******************************/
        //private const int frames6 = 9;
        ///*************** FIN ANIMAR SPRITES1 ****************/

        
        ///************* INICIO SCENE MANAGER ***************/
        ///*************** FIN SCENE MANAGER ****************/

  
        ///*************** FIN SCENE MANAGER ****************/


        ///***************************************** Seccion 0 *******************************************/
        //public Game1() {

        //    /************* INICIO ANIMAR SPRITES1 ***************/ 
        //    graphics = new GraphicsDeviceManager(this);
        //    Content.RootDirectory = "Content";
                       
        //    spritePos = new Vector2(graphics.PreferredBackBufferWidth / 2, 200);//las posicion del  sprite de imagenes
        //    SpriteTexture = new TexturaAnimada(spritePos, FRAMES, FRAMESPERSEC);
        //    /*****************Posicion y definicion de tiempo de aparicion********************************************************************/
        //    spritePos3 = new Vector2(graphics.PreferredBackBufferWidth / 2, 50);
        //    spritePos4 = new Vector2(150, 50);
        //    spritePos5 = new Vector2(50, 200);
        //    spritePos6 = new Vector2(150, 200);

        //    /*_____________________________Metodo de dibujado en pantalla_______________________________________________*/
        //    SpriteTexture3 = new TexturaAnimada(spritePos3, frames3, FRAMESPERSEC);
        //    SpriteTexture4 = new TexturaAnimada(spritePos4, frames4, FRAMESPERSEC);
        //    SpriteTexture5 = new TexturaAnimada(spritePos5, frames5, FRAMESPERSEC);
        //    SpriteTexture6 = new TexturaAnimada(spritePos6, frames6, FRAMESPERSEC);
        //    /*-----------------------------------------FIN--------------------------------------------------*/
        //    /*************** FIN ANIMAR SPRITES1 ****************/


        //}


        ///***************************************** Seccion 1 *******************************************/
        ///// <summary>
        ///// Allows the game to perform any initialization it needs to before starting to run.
        ///// This is where it can query for any required services and load any non-graphic
        ///// related content.  Calling base.Initialize will enumerate through any components
        ///// and initialize them as well.
        ///// </summary>
        //protected override void Initialize() {
        //    // TODO: Add your initialization logic here
        //    c=new Character(); //diego lo hizo
        //    base.Initialize();
        //}


        ///***************************************** Seccion 2 *******************************************/
        ///// <summary>
        ///// LoadContent will be called once per game and is the place to load
        ///// all of your content.
        ///// </summary>
        //protected override void LoadContent() {
        //    // Create a new SpriteBatch, which can be used to draw textures.
        //    spriteBatch = new SpriteBatch(GraphicsDevice);

        //    /************* INICIO ANIMAR SPRITES1 ***************/            
        //    // TODO: use this.Content to load your game content here
        //    // Create a new SpriteBatch, which can be used to draw textures.            
        //    spriteBatch3 = new SpriteBatch(GraphicsDevice);
        //    spriteBatch4 = new SpriteBatch(GraphicsDevice);
        //    spriteBatch5 = new SpriteBatch(GraphicsDevice);
        //    spriteBatch6 = new SpriteBatch(GraphicsDevice);

        //    //carga la sprite de imagenes
        //    SpriteTexture.Load(Content, "yeti1");
        //    SpriteTexture3.Load(Content, "yeti2");
        //    SpriteTexture4.Load(Content, "mario");
        //    SpriteTexture5.Load(Content, "bazooka");
        //    SpriteTexture6.Load(Content, "tirador");
        //    /*************** FIN ANIMAR SPRITES1 ****************/
        //}



        ///***************************************** Seccion 3 *******************************************/
        ///// <summary>
        ///// UnloadContent will be called once per game and is the place to unload
        ///// all content.
        ///// </summary>
        //protected override void UnloadContent() {
        //    // TODO: Unload any non ContentManager content here

        //}



        ///***************************************** Seccion 4 *******************************************/
        ///// <summary>
        ///// Allows the game to run logic such as updating the world,
        ///// checking for collisions, gathering input, and playing audio.
        ///// </summary>
        ///// <param name="gameTime">Provides a snapshot of timing values.</param>
        //protected override void Update(GameTime gameTime) {
                        
        //    // Allows the game to exit
        //    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        //        this.Exit();

        //    /************* INICIO ANIMAR SPRITES1 ***************/            
        //    // TODO: Add your update logic here
        //    // Allows the game to exit
        //    if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        //        this.Exit();

        //    // TODO: Add your update logic here
        //    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        //    SpriteTexture.UpdateFrame(elapsed);
        //    base.Update(gameTime);
        //    /*******************tiempo de transcurso del los sprite*****************************************************/
        //    float elapsed3 = (float)gameTime.ElapsedGameTime.TotalSeconds;
        //    SpriteTexture3.UpdateFrame(elapsed3);
        //    base.Update(gameTime);
        //    float elapsed4 = (float)gameTime.ElapsedGameTime.TotalSeconds;
        //    SpriteTexture4.UpdateFrame(elapsed4);
        //    base.Update(gameTime);
        //    float elapsed5 = (float)gameTime.ElapsedGameTime.TotalSeconds;
        //    SpriteTexture5.UpdateFrame(elapsed5);
        //    base.Update(gameTime);
        //    float elapsed6 = (float)gameTime.ElapsedGameTime.TotalSeconds;
        //    SpriteTexture6.UpdateFrame(elapsed6);
        //    base.Update(gameTime);
        //    /*************** FIN ANIMAR SPRITES1 ****************/
                       
        //}



        ///***************************************** Seccion 5 *******************************************/
        ///// <summary>
        ///// This is called when the game should draw itself.
        ///// </summary>
        ///// <param name="gameTime">Provides a snapshot of timing values.</param>
        //protected override void Draw(GameTime gameTime) {

        //    /************* INICIO ANIMAR SPRITES1 ***************/
        //    // TODO: Add your drawing code here
        //    GraphicsDevice.Clear(Color.CornflowerBlue);

        //    // TODO: Add your drawing code here
        //    spriteBatch.Begin();
        //    SpriteTexture.DrawFrame(spriteBatch);
        //    spriteBatch.End();

        //    spriteBatch3.Begin();
        //    SpriteTexture3.DrawFrame(spriteBatch3);
        //    spriteBatch3.End();

        //    spriteBatch4.Begin();
        //    SpriteTexture4.DrawFrame(spriteBatch4);
        //    spriteBatch4.End();

        //    spriteBatch5.Begin();
        //    SpriteTexture5.DrawFrame(spriteBatch5);
        //    spriteBatch5.End();

        //    spriteBatch6.Begin();
        //    SpriteTexture6.DrawFrame(spriteBatch6);
        //    spriteBatch6.End();
                   
        //    base.Draw(gameTime);
        //    /*************** FIN ANIMAR SPRITES1 ****************/


        //}

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        MenuScene escenaInicio;
        HelpScene escenaAyuda;
        GameScene escenaActiva;
        ActionScene escenaAccion;

        SpriteFont fuenteNormal;
        Texture2D fondo;
        KeyboardState newState;
        KeyboardState oldState;

        public Game1(){
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize() {
            base.Initialize();
        }

        protected override void LoadContent() {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            Services.AddService(typeof(ContentManager), Content);

            fuenteNormal = Content.Load<SpriteFont>("fuente");

            fondo = Content.Load<Texture2D>("fondo1");
            escenaInicio = new MenuScene(this, fuenteNormal, fondo);
            Components.Add(escenaInicio);

            fondo = Content.Load<Texture2D>("helpScreen");
            escenaAyuda = new HelpScene(this, fondo);
            Components.Add(escenaAyuda);

            escenaAccion = new ActionScene(this);
            Components.Add(escenaAccion);
            escenaAccion.Hide();

            escenaInicio.Show();
            escenaAyuda.Hide();

            escenaActiva = escenaInicio;
        }

        protected override void UnloadContent() {
            
        }

        protected override void Update(GameTime gameTime) {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            newState = Keyboard.GetState();

            if (escenaActiva == escenaInicio) {
                if (verificarTeclado(Keys.Enter) || verificarTeclado(Keys.Space)) {
                    switch (escenaInicio.IndiceSeleccionado) {
                        case 0:
                            escenaActiva.Hide();
                            escenaActiva = escenaAccion;
                            escenaActiva.Show();
                            break;
                        case 2:
                            escenaActiva.Hide();
                            escenaActiva = escenaAyuda;
                            escenaActiva.Show();
                            break;
                        case 3:
                            Exit();
                            break;
                    }
                }
            }
            else if (escenaActiva == escenaAccion) {
                    if (verificarTeclado(Keys.Space) || verificarTeclado(Keys.Enter) || verificarTeclado(Keys.Escape)) {
                        escenaActiva.Hide();
                        escenaActiva = escenaInicio;
                        escenaActiva.Show();
                    }
            }
            else if (escenaActiva == escenaAyuda) {
                    if (verificarTeclado(Keys.Space) || verificarTeclado(Keys.Enter) || verificarTeclado(Keys.Escape)) {
                        escenaActiva.Hide();
                        escenaActiva = escenaInicio;
                        escenaActiva.Show();
                    }
            }

            oldState = newState;
            base.Update(gameTime);
        }

        private bool verificarTeclado(Keys tecla) {
            return oldState.IsKeyDown(tecla) && newState.IsKeyUp(tecla);
        }

        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);
            base.Draw(gameTime);
            spriteBatch.End();
        }
                
    }
}
